(function($) {
    $.page=function(){
        var $page={};
		var pageGlobal={};
        $page.methods={
            init:function(){
				pageGlobal.deving=false;
				this.event();
				this.sliderCart();
				this.numChange();
			},	
			event:function(){
				$(".r3shoppingCartEdit").click(function(){
					if(!pageGlobal.deving){
						pageGlobal.dataid=$(this).attr("data-id");
						pageGlobal.deving=true;
						$page.methods.popupCall();
					}
				});
				$(".gobackHistoryBtn").click(function(){
					history.go(-1)
				});
			},
			numChange:function(){
				$('.copySelect select').change(function(){
					var num = $(this).val();
					if(num>1){
						$(this).parent().next().show();
					}
				})
			},
			popupEvent:function(){
				//select模拟
				try{$(".copySelectSize").copySelect({title:"尺寸:",defaultText:"选择尺寸",isDefault:true,optionClick:function(dom,index){}});}catch(e){} //select模拟
				try{$(".copySelect").copySelect({isDefault:true,optionClick:function(dom,index){}});}catch(e){} //select模拟
				//弹出层产品图片
				$page.slider.init();
				$page.zoom.init();		
			},
			popupCall:function(){
				var popurl=$common.dev.getPopUrl("r3scEditKey");
				new $.popup.show({url:popurl,
					close:true,
					complete:function(data){
						$("body").append(data);
						$page.methods.setShoppingCartEditDev($page.methods.setShoppingCartEditPro,pageGlobal.dataid);
					},
					error:function(){}
				});
			},
			setShoppingCartEditDev:function(callback,id){
				try{return setShoppingCartEditDev(callback,id);}catch(e){
//********************************************************************************************模拟开发环境********************************************************************************************//
                    var _imgArr=[
						{thumb:"images/product/product_60_60_1.jpg", image:"images/product/product_500_500_1.jpg"},
						{thumb:"images/product/product_60_60_2.jpg", image:"images/product/product_500_500_2.jpg"},
						{thumb:"images/product/product_60_60_3.jpg", image:"images/product/product_500_500_3.jpg"},
						{thumb:"images/product/product_60_60_4.jpg", image:"images/product/product_500_500_4.jpg"},
						{thumb:"images/product/product_60_60_5.jpg", image:"images/product/product_500_500_5.jpg"},
						{thumb:"images/product/product_60_60_6.jpg", image:"images/product/product_500_500_6.jpg"},
						{thumb:"images/product/product_60_60_1.jpg", image:"images/product/product_500_500_1.jpg"},
						{thumb:"images/product/product_60_60_2.jpg", image:"images/product/product_500_500_2.jpg"},
						{thumb:"images/product/product_60_60_3.jpg", image:"images/product/product_500_500_3.jpg"},
						{thumb:"images/product/product_60_60_4.jpg", image:"images/product/product_500_500_4.jpg"}
					];
					var _sizeArr=[
						{value:"95", option:"M"},
						{value:"96", option:"SM"},
						{value:"97", option:"LM"},
						{value:"98", option:"BL"},
						{value:"99", option:"X"}
					];
					var _pdtObj={
						name:"阿迪达斯@ 户外 男女 户外多功能鞋",
						code:"ITB@29",
						price:"8@69",
						oriprice:"13@69"
					};
					var _cartLogo='imgs/logo_25_20.png';
					var _currentSize=2; //0-n
					var _currentqty=5; //1-10,""
					var _abtn='<a href="javascript:;" title="保存@更改" class="btnCommon btnBlueLinear">保存更改</a>'
					callback({result:"success",imgArr:_imgArr, sizeArr:_sizeArr, pdtObj:_pdtObj, cartLogo:_cartLogo, currentSize:_currentSize, currentqty:_currentqty, abtn:_abtn});
                 };
			},
			setShoppingCartEditPro:function(data){
				if(data.result=="success"){
					//产品信息
					$(".popup.addShoppingcar .pdpTit").html(data.pdtObj.name+'<em>'+data.pdtObj.code+'</em>');
					$(".popup.addShoppingcar .pdpPrice").html('￥'+data.pdtObj.price+'<del>'+data.pdtObj.oriprice+'</del>');
					//选择表单
					//console.log(String(data.currentqty).length);
					if(String(data.currentqty).length>0){
						$(".popup.addShoppingcar .copySelectSize").show();
						$(".popup.addShoppingcar .copySelect").show();
						$(".popup.addShoppingcar .shouwan").hide();
						
						$(".popup.addShoppingcar .pdpSizeBox .copySelectSize.errorSelect select").empty();
						for(var s=0; s<data.sizeArr.length; s++){
							var _optionsize=$('<option value="'+data.sizeArr[s].value+'">'+data.sizeArr[s].option+'</option>');
							$(".popup.addShoppingcar .pdpSizeBox .copySelectSize.errorSelect select").append(_optionsize);
						}
						$(".popup.addShoppingcar .pdpSizeBox .copySelectSize.errorSelect select option").eq(data.currentSize).attr("selected", "selected");
						$(".popup.addShoppingcar .pdpSizeBox .copySelect select option").eq(data.currentqty-1).attr("selected", "selected");
					}else{
						$(".popup.addShoppingcar .copySelectSize").hide();
						$(".popup.addShoppingcar .copySelect").hide();
						$(".popup.addShoppingcar .shouwan").show();
					}
					//产品图片
					$(".popup.addShoppingcar .pdpSmallImgSlids").empty();
					for(var i=0; i<data.imgArr.length; i++){
						var _libox=$('<a href="javascript:;"><img src="'+data.imgArr[i].thumb+'" data-hero="'+data.imgArr[i].image+'"></a>');
						$(".popup.addShoppingcar .pdpSmallImgSlids").append(_libox);
					}
					if(data.cartLogo.length>0){
						$(".popup.addShoppingcar .pdpLogo").show();
						$(".popup.addShoppingcar .pdpLogo").attr("src",data.cartLogo);
					}else{
						$(".popup.addShoppingcar .pdpLogo").hide();
					}
					//默认显示第一页
					$(".popup.addShoppingcar .pdpSmallImgSlids a").eq(0).addClass("cur");
					$(".popup.addShoppingcar .skuBox .skuZoom").attr({src:data.imgArr[0].image});
					//按钮
					$(".popup.addShoppingcar .mt20").empty().html(data.abtn);
					//数据加载完成
					$(".popup.addShoppingcar").show();
					$page.methods.popupEvent();
					//新增
					if(String(data.currentqty).length>0){
						$(".popup.addShoppingcar .pdpSizeBox .copySelectSize.errorSelect .selectVal").html("尺寸:"+data.sizeArr[data.currentSize].option);
					}
					pageGlobal.deving=false;
				}
			},
			sliderCart:function(){
				$('.cartItem h4').bind('click', function() {
					$(this).next().slideToggle();
					$(this).find('.icon').toggleClass('icon-show');
					return false;
				})	
				$('.cartItem .slider').bind('click', function() {
					$(this).parent().next().slideToggle();
					$(this).find('.icon').toggleClass('icon-show');
					return false;
				})	
			},
			getPopupOptionDev:function(){
				//$common.page.getPopupOptionDev();
				var _obj=[];
				var _tempStr=$(".popup.addShoppingcar .copySelectSize .selectBox .selectVal").html();
				_tempStr=_tempStr.slice(3)
				_obj.push(_tempStr);
				_obj.push( $(".popup.addShoppingcar .copySelect .selectBox .selectVal").html() );
				_obj.push( $(".popup.addShoppingcar .copySelectSize select option:selected").val());
				return _obj;
			}
        };
        $page.slider={
            init:function(){
                //放大镜缩略图slider
                new touchslider('.pdpSmallImgSlids', {
                	nextButton: '.pdpSmallImgSlids-Next',
                	prevButton: '.pdpSmallImgSlids-Prev',
                	size:6,
                	scrollSize:6,
                	direction:'vertical',
                    moveFinishEvent:function(crtPage,totalPage){
                    	var items = $('.pdpSmallImgSlids').find("a").length;
                    	if(items<7){
                    		$('.pdpSmallImgSlids-Next,.pdpSmallImgSlids-Prev').hide();
                    	}
		                $('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Next").removeClass("unable");
		                $('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Prev").removeClass("unable");
		                if(crtPage==1){$('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Prev").addClass("unable");}
		                if(crtPage==totalPage){$('.pdpSmallImgdBox').find(".pdpSmallImgSlids-Next").addClass("unable");}
                    }
                });
            },
            //更新箭头样式
            arrowStyle:function(el,crtPage,totalPage){
                $(el).find(".nextH").removeClass("unable");
                $(el).find(".prevH").removeClass("unable");
                if(crtPage==1){$(el).find(".prevH").addClass("unable");}
                if(crtPage==totalPage){$(el).find(".nextH").addClass("unable");}
            }
        };
        //放大镜功能
        $page.zoom = {
            //操作事件
            event:function(){
                $(".pdpSmallImgSlids a").click(function(){$page.zoom.updataBigImage($(this)); return false;});//更新大图
            },
            //缩略图点击， 更新大图
            updataBigImage:function(el){
                this.isPlay=false;
                el.addClass("cur").siblings().removeClass("cur");
                $(".skuBox .skuZoom")[0].src=el.find("img").attr("data-hero");
            },
            //放大镜初始数据入口
            init:function(){
                this.event();				
            }
        };
        return $page.methods;
    };
})(jQuery);

